import React, { ReactElement, ReactNode, useState, Fragment } from 'react'
import { Upload, message } from 'antd';
import { LoadingOutlined, PlusOutlined } from '@ant-design/icons';
import Cook from 'js-cookie'
import { connect } from 'umi'

interface Props {
    children?: ReactNode,
    upLoad?: string,
    changeHeroInfo: Function,
    action: string,
    index?: number,
    changeSkin: Function,
    icon?: string,
    changeSkills: Function
}

function getBase64(img: any, callback: any) {
    const reader = new FileReader();
    reader.addEventListener('load', () => callback(reader.result));
    reader.readAsDataURL(img);
}

function beforeUpload(file: any) {
    const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
    if (!isJpgOrPng) {
        message.error('You can only upload JPG/PNG file!');
    }
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
        message.error('Image must smaller than 2MB!');
    }
    return isJpgOrPng && isLt2M;
}

function Index({ upLoad, changeHeroInfo, action, index, changeSkin, icon, changeSkills }: Props): ReactElement {
    const [loading, setLoading] = useState(false)
    const [imageUrl, setImageUrl] = useState("")

    const handleChange = (info: any) => {
        if (info.file.status === 'uploading') {
            setLoading(true);
            return;
        }
        if (info.file.status === 'done') {
            if (!icon) {
                if (index !== undefined) {
                    // 
                    changeSkin(index, 'img', info.file.response.url)
                } else {
                    changeHeroInfo(upLoad, info.file.response.url)
                }
            } else {
                changeSkills(index, icon, info.file.response.url)
            }

            getBase64(info.file.originFileObj, (imageUrl: string) => {
                setImageUrl(imageUrl)
                setLoading(false)
            });
        }
    };

    const uploadButton = (
        <div>
            {loading ? <LoadingOutlined /> : <PlusOutlined />}
            <div style={{ marginTop: 8 }}>Upload</div>
        </div>
    )
    return (
        <Fragment>
            <Upload
                listType="picture-card"
                className="avatar-uploader"
                showUploadList={false}
                action={"/api/upload/" + action}
                headers={{
                    Authorization: Cook.get('token')
                }}
                beforeUpload={beforeUpload}
                onChange={handleChange}
            >
                {imageUrl ? <img src={imageUrl} alt="avatar" style={{ width: '100%' }} /> : uploadButton}
            </Upload>
        </Fragment>
    )
}


export default connect(() => {
    return {

    }
}, (dispatch) => {
    return {
        changeHeroInfo(classify: string, value: string) {
            dispatch({ type: "hero/changeHeroInfo", classify, value })
        },
        changeSkin(index: number, classify: string, value: string) {
            dispatch({ type: "hero/changeSkin", index, classify, value })
        },
        changeSkills(index: number, classify: string, value: string) {
            dispatch({ type: "hero/changeSkills", index, classify, value })
        }
    }
})(Index)